// 置顶股票
function pushStockTop(stockCode, groupId) {
    $.ajax({
        url: `/stockGroup/pushTop/${groupId}/${stockCode}`,
        method: 'GET',
        success: function (res) {
            if (res.code === "0000") {
                loadGroupStocks(groupId);
                showToast('股票置顶成功', 'success');
            } else {
                showToast('置顶失败: ' + res.msg, 'error');
            }
        },
        error: function () {
            showToast('网络错误，请重试', 'error');
        }
    });
}

// 固定股票
function fixedStockTop(stockCode, groupId) {
    $.ajax({
        url: `/stockGroup/fixedTop/${groupId}/${stockCode}`,
        method: 'GET',
        success: function (res) {
            if (res.code === "0000") {
                loadGroupStocks(groupId);
                showToast('股票固定成功', 'success');
            } else {
                showToast('固定失败: ' + res.msg, 'error');
            }
        },
        error: function () {
            showToast('网络错误，请重试', 'error');
        }
    });
}

// 显示监控对话框
function showMonitorDialog(groupId, stockCode, stockName, costPrice, monitorPrice) {

    const dialog = $(`
        <div class="modal fade" id="monitorModal" tabindex="-1">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">添加监控 - ${stockName}</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="costPrice" class="form-label">成本价</label>
                            <input type="number" class="form-control" id="costPrice" step="0.01" value=${costPrice}>
                        </div>
                        <div class="mb-3">
                            <label for="monitorPrice" class="form-label">监视价</label>
                            <input type="number" class="form-control" id="monitorPrice" step="0.01" value=${monitorPrice}>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-primary" id="confirmMonitorBtn">确定</button>
                    </div>
                </div>
            </div>
        </div>
    `);

    $('body').append(dialog);
    $('#monitorModal').modal('show');

    // 确认监控按钮事件
    $('#confirmMonitorBtn').click(function () {
        const costPrice = $('#costPrice').val();
        const monitorPrice = $('#monitorPrice').val();

        if (!costPrice || !monitorPrice) {
            showToast('请填写完整的成本价和监视价', 'warning');
            return;
        }

        addMonitor(groupId, stockCode, costPrice, monitorPrice);
        $('#monitorModal').modal('hide');
    });

    // 模态框关闭后销毁
    $('#monitorModal').on('hidden.bs.modal', function () {
        $(this).remove();
    });
}

// 添加监控
function addMonitor(groupId, stockCode, costPrice, monitorPrice) {
    const stockDetail = {};
    stockDetail.groupId = groupId;
    stockDetail.code = stockCode;
    stockDetail.costPrice = parseFloat(costPrice);
    stockDetail.monitorPrice = parseFloat(monitorPrice);

    $.ajax({
        url: '/stockMonitor/add',
        method: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(stockDetail),
        success: function (res) {
            if (res.code === "0000") {
                showToast('监控设置成功', 'success');
            } else {
                showToast('监控设置失败: ' + res.msg, 'error');
            }
        },
        error: function () {
            showToast('网络错误，请重试', 'error');
        }
    });
}